'use strict';

const Controller = require('egg').Controller;

class ScoreController extends Controller {
  async detail() {
    const { ctx, app } = this;
    let id = ctx.params.id
        let data = await app.model.Score.findOne({
            where: {
                 id
            },
            include: [
              { model: this.ctx.model.User },
              { model: this.ctx.model.Class },
              { model: this.ctx.model.Branch },
            ],
        })
        data = JSON.parse(JSON.stringify(data))
        data.content = data.content.replace(/\/public\/uploads/g, this.config.webUrl + '/public/uploads')
        ctx.apiSuccess( data, 'ok', 200 );
  }

  async search() {
    const { ctx } = this
        let { data } = ctx.request.body;
        console.log('22222222222',data)
    // 查询数据库，获取学生姓名和科目对应的成绩
    const score = await ctx.service.score.searchScore(data.username, data.exam_id);
    if(score === null){
      ctx.apiFail(data="", '您所查询的成绩不存在，请正确选择查询条件', 400)
    }
    ctx.apiSuccess(score);
  }

}

module.exports = ScoreController;
